package com.asuka.service.impl;

import com.asuka.context.BaseContext;
import com.asuka.dto.ShoppingCartDTO;
import com.asuka.entity.Dish;
import com.asuka.entity.Setmeal;
import com.asuka.entity.ShoppingCart;
import com.asuka.mapper.DishMapper;
import com.asuka.mapper.SetmealMapper;
import com.asuka.mapper.ShoppingCartMapper;
import com.asuka.service.ShoppingCartService;
import com.asuka.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    @Transient
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();

        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);


        //设置userid
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //查询表里有无数据
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(shoppingCart);

        //数据存在 更新数量插入
        if(shoppingCarts != null && !shoppingCarts.isEmpty()){
            ShoppingCart shopping = shoppingCarts.get(0);
            shopping.setNumber(shopping.getNumber() + 1);

            shoppingCartMapper.update(shopping);
            return;
        }

        //不存在数据处理
        Long setmealId = shoppingCart.getSetmealId();




        //如果套餐id存在 单次添加购物车只能是菜品或套餐
        if(setmealId != null){
            //查询套餐的信息
            Setmeal setmeal = new Setmeal();
            setmeal.setId(setmealId);
            List<Setmeal> list = setmealMapper.selectSetmeal(setmeal);
            Setmeal s = list.get(0);

            //补充套餐的信息,插入
            shoppingCart.setName(s.getName());
            shoppingCart.setImage(s.getImage());
            shoppingCart.setAmount(s.getPrice());

        }else{
            //如果添加的是菜品
            //查询菜品
            Dish d = Dish.builder().id(shoppingCart.getDishId()).build();
            List<DishVO> list = dishMapper.selectList(d);
            DishVO dishVO = list.get(0);

            shoppingCart.setName(dishVO.getName());
            shoppingCart.setImage(dishVO.getImage());
            shoppingCart.setAmount(dishVO.getPrice());
        }

        //设置插入数据
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setNumber(1);

        shoppingCartMapper.insert(shoppingCart);
    }


    @Override
    public List<ShoppingCart> list() {


        ShoppingCart shoppingCart = new ShoppingCart();

        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(shoppingCart);

        return shoppingCarts;
    }


    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }
}
